/*
 * Copyright (c) 2021 Futurewei Technologies, Inc.
 *
 * clang2mpl is licensed under Mulan PSL v2.
 * You can use this software according to the terms and conditions of the Mulan
 * PSL v2. You may obtain a copy of Mulan PSL v2 at:
 *
 *     http://license.coscl.org.cn/MulanPSL2
 *
 * THIS SOFTWARE IS PROVIDED ON AN "AS IS" BASIS, WITHOUT WARRANTIES OF ANY
 * KIND, EITHER EXPRESS OR IMPLIED, INCLUDING BUT NOT LIMITED TO
 * NON-INFRINGEMENT, MERCHANTABILITY OR FIT FOR A PARTICULAR PURPOSE. See the
 * Mulan PSL v2 for more details.
 */
// RUN: %clang2mpl --ascii --verify %s -- --target=aarch64-linux-elf
// RUN: cat %m | %FileCheck %s

int foo(int a) {
  // CHECK: LOC 2 [[# @LINE + 3 ]]{{$}}
  // CHECK-NEXT: @beginning LOC 2 [[# @LINE + 3 ]]{{$}}
  // CHECK-NEXT: dassign %a 0 (add i32 (dread i32 %a, constval i32 1))
  beginning:
  ++a;
  if (a < 10)
    // CHECK: LOC 2 [[# @LINE + 2 ]]{{$}}
    // CHECK-NEXT: goto @beginning
    goto beginning;
  else
    // CHECK: LOC 2 [[# @LINE + 2 ]]{{$}}
    // CHECK-NEXT: goto @end
    goto end;
  // CHECK: LOC 2 [[# @LINE + 3 ]]{{$}}
  // CHECK-NEXT: @end LOC 2 [[# @LINE + 3 ]]{{$}}
  // CHECK-NEXT: return (dread i32 %a)
  end:
  return a;
}

int bar(int a) {
  // CHECK: LOC 2 [[#BLINE: @LINE + 2 ]]{{$}}
  // CHECK-NEXT: var %b <* void>
  void *b = &&bar1;
  // CHECK: LOC 2 [[#ELINE: @LINE + 2 ]]{{$}}
  // CHECK-NEXT: var %e <* void>
  void *e = &&bar2;

  // CHECK: LOC 2 [[#BLINE]]{{$}}
  // CHECK-NEXT: dassign %b 0 (addroflabel a64 @bar1)
  // CHECK: LOC 2 [[#ELINE]]{{$}}
  // CHECK-NEXT: dassign %e 0 (addroflabel a64 @bar2)

bar1:
  ++a;
  if (a < 10)
    // CHECK: LOC 2 [[#ELINE: @LINE + 2 ]]{{$}}
    // CHECK-NEXT: igoto (dread a64 %b)
    goto *b;
  else
    // CHECK: LOC 2 [[#ELINE: @LINE + 2 ]]{{$}}
    // CHECK-NEXT: igoto (dread a64 %e)
    goto *e;
bar2:
  return a;
}
